//
//  TZYGoodDetailViewController.m
//  Moment
//
//  Created by dllo on 15/12/26.
//  Copyright © 2015年 片刻. All rights reserved.
//

#import "TZYGoodDetailViewController.h"
#import "TZYGoodDetail.h"
#import "TZYGoodsDetailCell.h"
#define TableViewHeaderHeight 20 + 30 + 20 + SCREEN_HEIGHT / 4 / 3 + 20 + 20 + 30

@interface TZYGoodDetailViewController ()<UITableViewDataSource, UITableViewDelegate, UIWebViewDelegate>


@property (nonatomic, retain) TZYGoodDetail *thingDetail;
@property (nonatomic, retain) UILabel *groupName;
@property (nonatomic, retain) UILabel *titleL;
@property (nonatomic, retain) UILabel *userName;
@property (nonatomic, retain) UILabel *timeL;
@property (nonatomic, retain) UIImageView *userIcon;
@property (nonatomic, retain) UITableView *myTableView;
@property (nonatomic, assign) CGFloat height;
@property (nonatomic, strong) TZYGoodsDetailCell *cell;
@property (nonatomic, assign) int mark;
@property (nonatomic, strong) UILabel *fromLabel;


@end

@implementation TZYGoodDetailViewController

- (void)dealloc
{
    self.cell.webV.delegate = nil;
    self.cell.webV.scrollView.delegate = nil;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"详情";
    
    [self createTableView];
    
    //[self addHeaderRefresh];
    [self getAFNetWorkingData];
    self.mark = 0;
    
    UIImage *image = [UIImage imageNamed:@"back.png"];
    image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStyleDone target:self action:@selector(leftButtonAction:)];
    
}


- (void)leftButtonAction:(UIBarButtonItem *)sender
{
    [self.navigationController popViewControllerAnimated:YES];
    
}


#pragma mark -- 设置下拉刷新
-(void)addHeaderRefresh
{
    //当下拉时候会触发该方法
    self.myTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        NSLog(@"下拉");

        // 重新请求数据
        [self getAFNetWorkingData];
        
    }];
    
    // 进到应用自动刷新(可以放在viewdidload)
    [self.myTableView.mj_header beginRefreshing];
}

- (void)getAFNetWorkingData
{
    NSString *urlStr = @"http://api2.pianke.me/group/posts_info";
     NSString *para = [NSString stringWithFormat:@"contentid=%@&client=2&auth=C5FpQCTh8m3lDC8hyG83olTJxQMCiYOeIoj85CDxkcWP22NFPTBpc6m34", self.thingID];
    
    [GetData postUrl:urlStr body:para result:HXJSON requestStyle:HXRequestString headerFile:nil success:^(id result) {
        self.thingDetail = [[TZYGoodDetail alloc] init];
        
        NSDictionary *dic = [result objectForKey:@"data"];
        NSDictionary *tempDic = [dic objectForKey:@"postsinfo"];
        self.thingDetail.title = [tempDic objectForKey:@"title"];
        self.thingDetail.addtime_f = [tempDic objectForKey:@"addtime_f"];
        self.thingDetail.html = [tempDic objectForKey:@"html"];
        NSDictionary *userDic = [tempDic objectForKey:@"userinfo"];
        self.thingDetail.uname = [userDic objectForKey:@"uname"];
        self.thingDetail.icon = [userDic objectForKey:@"icon"];
        self.thingDetail.uid = [userDic objectForKey:@"uid"];
        NSDictionary *groupDic = [tempDic objectForKey:@"groupInfo"];
        self.thingDetail.fromTitle = [groupDic objectForKey:@"title"];
        self.thingDetail.groupid = [groupDic objectForKey:@"groupid"];
        NSDictionary *shareDic = [tempDic objectForKey:@"shareinfo"];
        self.thingDetail.url = [shareDic objectForKey:@"url"];
        [self setValue];
        [self.myTableView.mj_header endRefreshing];
        [self.myTableView reloadData];
        
        
        
    } failure:^(NSError *error) {
        
        
    }];

}


//给头部赋值
- (void)setValue
{
    self.groupName.text = self.thingDetail.fromTitle;
    self.groupName.textColor = [UIColor colorWithRed:138 / 255.0 green:191 / 255.0 blue:102 / 255.0 alpha:1.000];
    self.groupName.font = [UIFont systemFontOfSize:[TZYFontSize getFontSizeForSmall]];
    self.titleL.text = self.thingDetail.title;
    self.userName.text = self.thingDetail.uname;
    self.timeL.text = self.thingDetail.addtime_f;
    self.timeL.textColor = [UIColor colorWithRed:138 / 255.0 green:191 / 255.0 blue:102 / 255.0 alpha:1.000];
    [self.userIcon sd_setImageWithURL:[NSURL URLWithString:self.thingDetail.icon]];
    
}


- (void)createTableView
{
    self.myTableView = [[UITableView alloc] init];
    [self.view addSubview:self.myTableView];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    //self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.myTableView.showsVerticalScrollIndicator = NO;
    self.myTableView.backgroundColor = [UIColor whiteColor];
    
    [self.myTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(@0);
    }];
    
    
    //Row的高
    self.myTableView.rowHeight = 500;
    

//    [self.myTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.myTableView registerClass:[TZYGoodsDetailCell class] forCellReuseIdentifier:@"TZYGoodsDetailCell"];
    
    
    UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, TableViewHeaderHeight)];
    //backView.backgroundColor = [UIColor redColor];
    self.myTableView.tableHeaderView = backView;
    
    
    self.fromLabel = [[UILabel alloc] init];
    //self.fromLabel.backgroundColor = [UIColor orangeColor];
    [backView addSubview:self.fromLabel];
    
    [self.fromLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(@20);
        make.left.mas_equalTo(@10);
        make.width.mas_equalTo(@40);
        make.height.mas_equalTo(@20);
        
    }];
    
    self.fromLabel.text = @"from:";
    self.fromLabel.font = [UIFont systemFontOfSize:[TZYFontSize getFontSizeForSmall]];
    
    
    self.groupName = [[UILabel alloc] init];
    //self.groupName.backgroundColor = [UIColor purpleColor];
    self.groupName.font = [UIFont boldSystemFontOfSize:[TZYFontSize getFontSizeForMiddle]];
    [backView addSubview:self.groupName];
    
    [self.groupName mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(@20);
        make.width.mas_equalTo(@((SCREEN_WIDTH - 40) * (3.0 / 5)));
        make.left.mas_equalTo(self.fromLabel.mas_right).offset(5);
        make.height.mas_equalTo(@20);
        
    }];
    
    self.titleL = [[UILabel alloc] init];
    //self.titleL.backgroundColor = [UIColor yellowColor];
    self.titleL.numberOfLines = 0;
    self.titleL.font = [UIFont boldSystemFontOfSize:[TZYFontSize getFontSizeForBig]];
    [backView addSubview:self.titleL];
    
    [self.titleL mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.fromLabel.mas_bottom).offset(20);
        make.right.mas_equalTo(@(-20));
        make.left.mas_equalTo(@20);
        make.height.mas_equalTo(@(SCREEN_HEIGHT / 4 / 3));
        
    }];
    
    
    
    
    self.userIcon = [[UIImageView alloc] init];
    //self.userIcon.backgroundColor = [UIColor yellowColor];
    [backView addSubview:self.userIcon];
    
    [self.userIcon mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.titleL.mas_bottom).offset(5);
        make.left.mas_equalTo(@20);
        make.width.mas_equalTo(@50);
        make.height.mas_equalTo(@50);
        
    }];
    
    
    self.userIcon.layer.masksToBounds = YES;
    //设置半径
    self.userIcon.layer.cornerRadius = 50 / 2.0;
    //边框宽度
    self.userIcon.layer.borderWidth = 0;
    
    
    
    UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGR:)];
    self.userIcon.userInteractionEnabled = YES;
    [self.userIcon addGestureRecognizer:tapGR];
    
    
    
    
    
    self.userName = [[UILabel alloc] init];
    self.userName.font = [UIFont boldSystemFontOfSize:[TZYFontSize getFontSizeForMiddle]];
    //self.userName.backgroundColor = [UIColor orangeColor];
    [backView addSubview:self.userName];
    
    [self.userName mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.userIcon);
        make.left.mas_equalTo(self.userIcon.mas_right).offset(10);
        make.width.mas_equalTo(@((SCREEN_WIDTH - 80) / 2));
        make.height.mas_equalTo(@50);
        
    }];
    

    
    self.timeL = [[UILabel alloc] init];
    //self.timeL.backgroundColor = [UIColor yellowColor];
    self.timeL.font = [UIFont boldSystemFontOfSize:[TZYFontSize getFontSizeForSmall]];
    [backView addSubview:self.timeL];
    
    [self.timeL mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.userName);
        make.width.mas_equalTo(@120);
        make.right.mas_equalTo(@(-20));
        make.height.mas_equalTo(@50);
        
    }];
    
    

    self.cell.webV.delegate = self;
   
}



#pragma mark-轻拍图片
- (void)tapGR:(UIGestureRecognizer *)sender
{
    
    TZYHeaderWebViewController *headerWebVC = [[TZYHeaderWebViewController alloc] init];
    headerWebVC.str = [NSString stringWithFormat:@"%@", self.thingDetail.uid];
    
    [self presentViewController:headerWebVC animated:YES completion:^{
        
        
    }];
    
    //[self.navigationController pushViewController:headerWebVC animated:YES];
    
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return 1;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"TZYGoodsDetailCell";
    self.cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    
    //self.cell.backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1.0];
    
    
    [self.cell.webV loadHTMLString:[NSString stringWithFormat:@"<head><style>img{max-width:275px !important;}</style><body style=\"text-indent:2em;\%@", self.thingDetail.html] baseURL:nil];
    
    self.height = [self reloadFrameWithWeb:self.cell.webV];
    
    
    return self.cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return self.height;
}


- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    
    [self.myTableView reloadData];
    self.mark++;
}

-(void)webViewDidStartLoad:(UIWebView *)webView{
    
    
//    if (_mark == 2) {
//        [webView stopLoading];
//    }
}



- (CGFloat)reloadFrameWithWeb:(UIWebView *)web
{
    
    CGRect frame = web.frame;
    frame.size.height = web.scrollView.contentSize.height;
    web.frame = frame;
    return web.frame.size.height;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
